window.onload = initialize;

function initialize() {
	var nextPhoto1 = document.getElementById('nextPhoto1');
	var nextPhoto2 = document.getElementById('nextPhoto2');
	
	var previousPhoto1 = document.getElementById('previousPhoto1');
	var previousPhoto2 = document.getElementById('previousPhoto2');
	
	var caption = document.getElementById('printCaption').firstChild;
	var photoId = document.getElementById('getPhotoId');
	
	nextPhoto1.onclick = nextPhotoLookup;
	nextPhoto2.onclick = nextPhotoLookup;
	
	previousPhoto1.onclick = previousPhotoLookup;
	previousPhoto2.onclick = previousPhotoLookup;
	
	request = null;
	createRequest();
}

function nextPhotoLookup() {
	
	if(document.getElementById('getAlbumId')){
		var aid = document.getElementById('getAlbumId').value
		var url="http://info230.cs.cornell.edu/users/aballantine/www/p3/scripts/nextPhoto.php?aid="+aid+"&pid=";
	}else{
		var url="http://info230.cs.cornell.edu/users/aballantine/www/p3/scripts/nextPhoto.php?pid=";
	}
	
	var photoId = document.getElementById('getPhotoId').value;
	
	request.open("GET", url+photoId, true);
	request.onreadystatechange = processPhoto;
	
	request.send(null);
    return false;
}

function previousPhotoLookup() {
	
	if(document.getElementById('getAlbumId')){
		var aid = document.getElementById('getAlbumId').value
		var url="http://info230.cs.cornell.edu/users/aballantine/www/p3/scripts/nextPhoto.php?back=true&aid="+aid+"&pid=";
	}else{
		var url="http://info230.cs.cornell.edu/users/aballantine/www/p3/scripts/nextPhoto.php?back=true&pid=";
	}
	
	var previousPhotoId = document.getElementById('previousPhotoId').value;
	
	/* Set up the Ajax call */
	request.open("GET", url+previousPhotoId, true);
	request.onreadystatechange = processPhoto;
	
	/* Make the Ajax call */
	request.send(null);
    return false;
}


function processPhoto(){
	var caption = document.getElementById('printCaption').firstChild;
	var photo = document.getElementById('photo');
	
	if (request.readyState == 4){
		if(request.responseText == ''){
			caption.nodeValue = "There was an error. Please refresh the page.";
		}else{
			photoInfo = eval(request.responseText); 
		
			var previousPhotoId = document.getElementById('previousPhotoId');
			var photoId = document.getElementById('getPhotoId');
			var alterPhotoLink = document.getElementById('alterPhotoLink');
			
			caption.nodeValue = getCaption;
			photo.src = link;
			photo.setAttribute('width', imageWidth);
			
			if(attribute == "photoDisplay"){
				photo.setAttribute('class', 'photoDisplay');
			}else{
				photo.setAttribute('class', 'viewPhoto');
			}
			previousPhotoId.value = (getpid - 1);
			photoId.value = (getpid + 1);
			alterPhotoLink.href = "http://info230.cs.cornell.edu/users/aballantine/www/p3/admin/alterPhoto.php?pid=" + getpid +"";
		}
	}else{
		caption.nodeValue = "Loading...";
	}
}
function createRequest() {

	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	
	if (request == null) {
		alert("Error creating request object!");
	}
}
